﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading;

namespace csharpcore7_ch6
{
    class ToStringAndParse
    {
        static void _Main()
        {
            string s = true.ToString();     // s = "True"
            Console.WriteLine(s);
            bool b = bool.Parse(s);         //b = True;
            Console.WriteLine(b);

            int i;
            i = int.Parse("123");
            //i = int.Parse("abc");//抛出异常
            bool failure = int.TryParse("qwerty", out i); 
            bool success = int.TryParse("123", out i);

            Console.WriteLine(double.Parse("1.234"));

            CultureInfo culture = CultureInfo.CreateSpecificCulture("de");
            Thread.CurrentThread.CurrentCulture = culture;
            CultureInfo.CurrentCulture = culture; //设置当前的文化为新的文化
            CultureInfo.CurrentUICulture = culture;
            Console.WriteLine("The current culture is now {0}",
                      CultureInfo.CurrentCulture.Name);
            Console.WriteLine("The current uiculture is now {0}",
                    CultureInfo.CurrentCulture.Name);

            //在德国，"."表示千位分隔而不是小数点。而指定一个不变文件就可以解决。
           // double x = double.Parse("1.234", CultureInfo.DefaultThreadCurrentCulture); //1234 (在德国)
            double x = double.Parse("1.234", CultureInfo.CurrentUICulture); //1234 (在德国)
            double y = double.Parse("1.234", CultureInfo.InvariantCulture);

            Console.WriteLine(x);
            Console.WriteLine(y);

            //string y1 = 1.234.ToString(CultureInfo.InvariantCulture);
            string y1 = 1.234.ToString();
            Console.WriteLine(y1);


            //de-- 德语
            //de - AT-- 德语(奥地利)
            //de - BE-- Deutsch(Belgien)
            //de - CH-- 德语(瑞士)
            //de - DE-- 德语(德国)
            //de - IT-- Deutsch(Italien)
            //de - LI-- 德语(列支敦士登)
            //de - LU-- 德语(卢森堡)

            // Get all custom cultures.
            //CultureInfo[] custom = CultureInfo.GetCultures(CultureTypes.UserCustomCulture);
            //if (custom.Length == 0)
            //{
            //    Console.WriteLine("There are no user-defined custom cultures.");
            //}
            //else
            //{
            //    Console.WriteLine("Custom cultures:");
            //    foreach (var c in custom)
            //        Console.WriteLine("   {0} -- {1}", c.Name, c.DisplayName);
            //}
            //Console.WriteLine();

        }
    }
}
